here are additional examples of mixed nested loops in Java:
While loop inside Do-While loop
Pattern printing using While loop inside Do-While loop
public class Main {
public static void main(String[] args) {
int i = 0;
do { // Outer do-while loop
int j = 0;
while(j < 5) { // Inner while loop
System.out.print("* ");
j++;
}
System.out.println();
i++;
} while(i < 5);
}
}
Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Do-While loop inside While loop
Pattern printing using Do-While loop inside While loop
public class Main {
public static void main(String[] args) {
int i = 0;
while(i < 5) { // Outer while loop
int j = 0;
do { // Inner do-while loop
System.out.print("* ");
j++;
} while(j < 5);
System.out.println();
i++;
}
}
}
Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
For loop inside While loop inside For loop
Pattern printing using For loop inside While loop inside For loop
public class Main {
public static void main(String[] args) {
for(int i = 0; i < 3; i++) { // Outer for loop
int j = 0;
while(j < 3) { // Middle while loop
for(int k = 0; k < 3; k++) { // Inner for loop
System.out.print("* ");
}
System.out.println();
j++;
}
System.out.println();
}
}
}
Output
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
Do-While loop inside For loop inside While loop
Pattern printing using Do-While loop inside For loop inside While loop
public class Main {
public static void main(String[] args) {
int i = 0;
while(i < 3) { // Outer while loop
for(int j = 0; j < 3; j++) { // Middle for loop
int k = 0;
do { // Inner do-while loop
System.out.print("* ");
k++;
} while(k < 3);
System.out.println();
}
System.out.println();
i++;
}
}
}
Output
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
While loop inside Do-While loop inside For loop
Pattern printing using While loop inside Do-While loop inside For loop
public class Main {
public static void main(String[] args) {
for(int i = 0; i < 3; i++) { // Outer for loop
int j = 0;
do { // Middle do-while loop
int k = 0;
while(k < 3) { // Inner while loop
System.out.print("* ");
k++;
}
System.out.println();
j++;
} while(j < 3);
System.out.println();
}
}
}
Output
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
In each of these examples, the innermost loop executes its iterations for each iteration of the middle loop, which in turn executes its iterations for each iteration of the outermost loop. This results in a cube pattern of asterisks.